package example.grpc.controller;

import com.bao.grpc.lib.UserGrpc;
import com.bao.grpc.lib.UserRequest;
import com.bao.grpc.lib.UserResponse;
import com.google.common.base.Stopwatch;
import example.grpc.HelloRequest;
import example.grpc.HelloResponse;
import example.grpc.HelloServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class UserController {

    @GetMapping("/getUser")
    public String getUser(String name){
        ManagedChannel channel = ManagedChannelBuilder.forAddress("127.0.0.1", 8081)
                .usePlaintext()
                .build();
        Stopwatch stopwatch = Stopwatch.createStarted();
        UserGrpc.UserBlockingStub stub = UserGrpc.newBlockingStub(channel);
        UserResponse userResponse = stub.getUser(UserRequest.newBuilder().setName(name).build());
        stopwatch.stop();
        channel.shutdown();
        return userResponse.getMessage();
    }


}
